import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**战报-施法基础类属性枚举*/
export const enum E_BriSkillEffectBase {
	/**释放角色id*/
	SEND_ACTOR_ID = 0,
	/**接收角色id*/
	RECV_ACTOR_ID = 1,
	/**技能id*/
	SKILL_ID = 2,
	/**技能配置id*/
	SKILL_CONFIG_ID = 3,
	/**效果id(可能为0)*/
	EFFECT_ID = 4,
	/**效果配置id*/
	EFFECT_CONFIG_ID = 5,
	/**枚举数量*/
	EnumCount = 6
}


/**战报-施法基础类*/
export class BriSkillEffectBase {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_BRI_SKILL_EFFECT_BASE_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_BRI_SKILL_EFFECT_BASE_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: BriSkillEffectBase[] = [];

	/**创建入口*/
	public static create(json?: string): BriSkillEffectBase {
		const c = this.POOL.pop() || new BriSkillEffectBase();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**释放角色id (uint16)*/
	protected _sendActorId: number;
	/**接收角色id (uint16)*/
	protected _recvActorId: number;
	/**技能id (uint32)*/
	protected _skillId: number;
	/**技能配置id (uint32)*/
	protected _skillConfigId: number;
	/**效果id(可能为0) (uint32)*/
	protected _effectId: number;
	/**效果配置id (uint32)*/
	protected _effectConfigId: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (BriSkillEffectBase.POOL.indexOf(this) === -1) {
			BriSkillEffectBase.POOL.push(this);
		}
	}

	/**释放角色id*/
	public get sendActorId() {
		return this._sendActorId;
	}


	/**释放角色id*/
	public set sendActorId(v: number) {
		this._sendActorId = v;
	}


	/**接收角色id*/
	public get recvActorId() {
		return this._recvActorId;
	}


	/**接收角色id*/
	public set recvActorId(v: number) {
		this._recvActorId = v;
	}


	/**技能id*/
	public get skillId() {
		return this._skillId;
	}


	/**技能id*/
	public set skillId(v: number) {
		this._skillId = v;
	}


	/**技能配置id*/
	public get skillConfigId() {
		return this._skillConfigId;
	}


	/**技能配置id*/
	public set skillConfigId(v: number) {
		this._skillConfigId = v;
	}


	/**效果id(可能为0)*/
	public get effectId() {
		return this._effectId;
	}


	/**效果id(可能为0)*/
	public set effectId(v: number) {
		this._effectId = v;
	}


	/**效果配置id*/
	public get effectConfigId() {
		return this._effectConfigId;
	}


	/**效果配置id*/
	public set effectConfigId(v: number) {
		this._effectConfigId = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_BriSkillEffectBase): any {
		switch (k) {
			case E_BriSkillEffectBase.SEND_ACTOR_ID: return this._sendActorId;
			case E_BriSkillEffectBase.RECV_ACTOR_ID: return this._recvActorId;
			case E_BriSkillEffectBase.SKILL_ID: return this._skillId;
			case E_BriSkillEffectBase.SKILL_CONFIG_ID: return this._skillConfigId;
			case E_BriSkillEffectBase.EFFECT_ID: return this._effectId;
			case E_BriSkillEffectBase.EFFECT_CONFIG_ID: return this._effectConfigId;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_BriSkillEffectBase, v: any): void {
		switch (k) {
			case E_BriSkillEffectBase.SEND_ACTOR_ID: this._sendActorId = v; break;
			case E_BriSkillEffectBase.RECV_ACTOR_ID: this._recvActorId = v; break;
			case E_BriSkillEffectBase.SKILL_ID: this._skillId = v; break;
			case E_BriSkillEffectBase.SKILL_CONFIG_ID: this._skillConfigId = v; break;
			case E_BriSkillEffectBase.EFFECT_ID: this._effectId = v; break;
			case E_BriSkillEffectBase.EFFECT_CONFIG_ID: this._effectConfigId = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//释放角色id
		this._sendActorId = 0;
		//接收角色id
		this._recvActorId = 0;
		//技能id
		this._skillId = 0;
		//技能配置id
		this._skillConfigId = 0;
		//效果id(可能为0)
		this._effectId = 0;
		//效果配置id
		this._effectConfigId = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._sendActorId) {
			json.sendActorId = this._sendActorId;
		}
		if (this._recvActorId) {
			json.recvActorId = this._recvActorId;
		}
		if (this._skillId) {
			json.skillId = this._skillId;
		}
		if (this._skillConfigId) {
			json.skillConfigId = this._skillConfigId;
		}
		if (this._effectId) {
			json.effectId = this._effectId;
		}
		if (this._effectConfigId) {
			json.effectConfigId = this._effectConfigId;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.sendActorId) {
			this._sendActorId = json.sendActorId;
		}
		if (json.recvActorId) {
			this._recvActorId = json.recvActorId;
		}
		if (json.skillId) {
			this._skillId = json.skillId;
		}
		if (json.skillConfigId) {
			this._skillConfigId = json.skillConfigId;
		}
		if (json.effectId) {
			this._effectId = json.effectId;
		}
		if (json.effectConfigId) {
			this._effectConfigId = json.effectConfigId;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint16(this._sendActorId);
		byte.writeUint16(this._recvActorId);
		byte.writeUint32(this._skillId);
		byte.writeUint32(this._skillConfigId);
		byte.writeUint32(this._effectId);
		byte.writeUint32(this._effectConfigId);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._sendActorId = byte.readUint16();
		this._recvActorId = byte.readUint16();
		this._skillId = byte.readUint32();
		this._skillConfigId = byte.readUint32();
		this._effectId = byte.readUint32();
		this._effectConfigId = byte.readUint32();
		return this;
	}


	/**克隆*/
	public clone(): BriSkillEffectBase {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = BriSkillEffectBase.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = BriSkillEffectBase.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//释放角色id
			if (idx === E_BriSkillEffectBase.SEND_ACTOR_ID) {
				const oldv = this._sendActorId;
				this._sendActorId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._sendActorId, oldv);
				i += 2;
				continue;
			}
			//接收角色id
			if (idx === E_BriSkillEffectBase.RECV_ACTOR_ID) {
				const oldv = this._recvActorId;
				this._recvActorId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._recvActorId, oldv);
				i += 2;
				continue;
			}
			//技能id
			if (idx === E_BriSkillEffectBase.SKILL_ID) {
				const oldv = this._skillId;
				this._skillId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._skillId, oldv);
				i += 2;
				continue;
			}
			//技能配置id
			if (idx === E_BriSkillEffectBase.SKILL_CONFIG_ID) {
				const oldv = this._skillConfigId;
				this._skillConfigId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._skillConfigId, oldv);
				i += 2;
				continue;
			}
			//效果id(可能为0)
			if (idx === E_BriSkillEffectBase.EFFECT_ID) {
				const oldv = this._effectId;
				this._effectId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._effectId, oldv);
				i += 2;
				continue;
			}
			//效果配置id
			if (idx === E_BriSkillEffectBase.EFFECT_CONFIG_ID) {
				const oldv = this._effectConfigId;
				this._effectConfigId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._effectConfigId, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in BriSkillEffectBase with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: BriSkillEffectBase): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._sendActorId !== other._sendActorId) {
			updates.push(E_BriSkillEffectBase.SEND_ACTOR_ID, this._sendActorId);
		}
		if (this._recvActorId !== other._recvActorId) {
			updates.push(E_BriSkillEffectBase.RECV_ACTOR_ID, this._recvActorId);
		}
		if (this._skillId !== other._skillId) {
			updates.push(E_BriSkillEffectBase.SKILL_ID, this._skillId);
		}
		if (this._skillConfigId !== other._skillConfigId) {
			updates.push(E_BriSkillEffectBase.SKILL_CONFIG_ID, this._skillConfigId);
		}
		if (this._effectId !== other._effectId) {
			updates.push(E_BriSkillEffectBase.EFFECT_ID, this._effectId);
		}
		if (this._effectConfigId !== other._effectConfigId) {
			updates.push(E_BriSkillEffectBase.EFFECT_CONFIG_ID, this._effectConfigId);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(BriSkillEffectBase.TOTAL_EVENT_ID, this.getV(0));
	}

}
